home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / timer.c < prev    next >
C/C++ Source or Header  |  1994-10-12  |  3KB  |  137 lines

  1. /* 
  2. Copyright (C) 1990, 1992 Free Software Foundation
  3.     written by Doug Lea (dl@rocky.oswego.edu)
  4.  
  5. This file is part of the GNU C++ Library.  This library is free
  6. software; you can redistribute it and/or modify it under the terms of
  7. the GNU Library General Public License as published by the Free
  8. Software Foundation; either version 2 of the License, or (at your
  9. option) any later version.  This library is distributed in the hope
  10. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  11. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE.  See the GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17.  
  18. #ifdef __GNUG__
  19. #pragma implementation
  20. #endif
  21.  
  22. /* Timing functions from Doug Schmidt... */
  23.  
  24. /* no such thing as "negative time"! */
  25. #define  TIMER_ERROR_VALUE -1.0   
  26.  
  27. /* If this does not work on your system, change this to #if 0, and 
  28.    report the problem. */
  29.  
  30. #if 1
  31.  
  32. #include <_G_config.h>
  33. #if _G_HAVE_SYS_RESOURCE
  34. #include <sys/time.h>
  35. #include <sys/resource.h>
  36. #endif
  37. #if !_G_HAVE_SYS_RESOURCE || !defined(RUSAGE_SELF)
  38. #define USE_TIMES
  39. #include <sys/param.h>
  40. #include <sys/types.h>
  41. #include <sys/times.h>
  42. #if !defined (HZ) && defined(CLK_TCK)
  43. #define HZ CLK_TCK
  44. #endif
  45. static struct tms Old_Time;
  46. static struct tms New_Time;
  47. #else
  48. static struct rusage Old_Time;
  49. static struct rusage New_Time;
  50. #endif
  51. static int    Timer_Set = 0;
  52.  
  53. double
  54. start_timer()
  55. {
  56.    Timer_Set = 1;
  57. #ifdef USE_TIMES
  58.    times(&Old_Time);
  59.    return((double) Old_Time.tms_utime / HZ);
  60. #else
  61.    getrusage(RUSAGE_SELF,&Old_Time);        /* set starting process time */
  62.    return(Old_Time.ru_utime.tv_sec + (Old_Time.ru_utime.tv_usec / 1000000.0));
  63. #endif
  64. }
  65.  
  66. /* Returns process time since Last_Time.
  67.    If parameter is 0.0, returns time since the Old_Time was set.
  68.    Returns TIMER_ERROR_VALUE if `start_timer' is not called first.  */
  69.  
  70. double
  71. return_elapsed_time(Last_Time)
  72.      double Last_Time;
  73. {
  74.    if (!Timer_Set) {
  75.       return(TIMER_ERROR_VALUE);
  76.    }   
  77.    else {
  78.     /* get process time */
  79. #ifdef USE_TIMES
  80.       times(&New_Time);
  81. #else
  82.       getrusage(RUSAGE_SELF,&New_Time);
  83. #endif
  84.       if (Last_Time == 0.0) {
  85. #ifdef USE_TIMES
  86.      return((double) (New_Time.tms_utime - Old_Time.tms_utime) / HZ);
  87. #else
  88.          return((New_Time.ru_utime.tv_sec - Old_Time.ru_utime.tv_sec) + 
  89.                ((New_Time.ru_utime.tv_usec - Old_Time.ru_utime.tv_usec) 
  90.                 / 1000000.0));
  91. #endif
  92.       }
  93.       else {
  94. #ifdef USE_TIMES
  95.      return((double) New_Time.tms_utime / HZ - Last_Time);
  96. #else
  97.          return((New_Time.ru_utime.tv_sec + 
  98.                 (New_Time.ru_utime.tv_usec / 1000000.0)) - Last_Time);
  99. #endif
  100.       }
  101.    }
  102. }
  103.  
  104. #ifdef VMS
  105. void sys$gettim(unsigned int*) asm("sys$gettim");
  106.  
  107. getrusage(int dummy,struct rusage* time){
  108.     double rtime;
  109.     unsigned int systime[2];
  110.     int i;
  111.     sys$gettim(&systime[0]);
  112.     rtime=systime[1];
  113.     for(i=0;i<4;i++) rtime *= 256;
  114.     rtime+= systime[0];
  115. /* we subtract an offset to make sure that the number fits in a long int*/
  116.     rtime=rtime/1.0e+7-4.144e+9;
  117.     time->ru_utime.tv_sec= rtime;
  118.     rtime=(rtime-time->ru_utime.tv_sec)*1.0e6;    
  119.     time->ru_utime.tv_usec= rtime;
  120. }
  121. #endif
  122. #else /* dummy them out */
  123.  
  124. double start_timer()
  125. {
  126.   return TIMER_ERROR_VALUE;
  127. }
  128.  
  129. double return_elapsed_time(double)
  130. {
  131.   return TIMER_ERROR_VALUE;
  132. }
  133.  
  134. #endif /* timing stuff */
  135.  
  136.  
  137.